$(function () {
    init_mudal();
});
/**初始化模板**/
function init_mudal() {
    /**初始化部门树状图**/
    $.post(ctx+"dept/getDeptToJSTree",{},function (r) {
        var array  = r.msg;
        $("#deptjsTree_json").jstree({
            "core": {"multiple": true,"data":array},
            "checkbox" : {"keep_selected_style" : false},
            "plugins" : [ "wholerow", "checkbox" ]
        });
        // 获得点击节点的id
        $('#deptjsTree_json').on("changed.jstree", function (e, data) {
            var exConfigUuid = $("#exConfig_form_id").val();
            //if (exConfigUuid == "" || exConfigUuid == undefined){$JuXue.n_danger("请选择左侧得意向项目");return ;}
            var nodeid = data.node.id;
            var text = data.node.text;
            var action = data.action;
            if (action == "deselect_node"){
                $("#dept"+nodeid).remove();}else{
                var  divapent = '<div class="input-group col-md-8 col-sm-8 m-3" id="dept'+nodeid+'" cuid ="1" data-id="'+nodeid+'">\n' +
                    '<span class="input-group-addon">'+text+'</span>' +
                    '<input type="text" class="form-control" value="0" placeholder="填写本月得分配数字"/>\n' +
                    '<span class="input-group-addon"><a href="#" onclick="downMove(this)"><i class="fa fa-arrow-circle-down"></i></a></span>' +
                    '<span class="input-group-addon"><a href="#" onclick="upMove(this)"><i class="fa fa-arrow-circle-up"></i></a></span>' +
                    '<span class="input-group-addon"><a href="#" >已经分配:</a></span>' +
                    '<span class="input-group-addon"><a href="#" >0 个</a></span>' +
                    '<span class="input-group-addon">月度计划:</span>' +
                    '<input type="text" class="form-control" value="0" placeholder="填写本月得分配总数字"/>\n' +
                    '</div>';
                $("#exConfig_form").append(divapent);
            }
        });
        /**初始化推送部门配置**/
        $.each(array,function (index,val) {
            $("#deptListExt").append('<div class="fc-event ui-draggable" ><a href="#" onclick="selectDeptEx(this)" data="'+val.id+'">'+val.text+'</a></div>');
        });
    });

    sessionStorage.setItem("selectDept","");
    sessionStorage.setItem("selectIntern","");
}

function chooseFen(btn) {
    $.post(ctx+"exModal/exModalAll",{"exCfgType":btn},function (r) {
        if (r.code == 0){
            var alldata = r.msg;$("#exadd").empty();
            $.each(alldata,function (index,val) {
                $("#exadd").append("<div class='fc-event'><a onclick='selectIntern(this);' type='"+btn+"' data-id="+val.exCfgUuid+" >"+val.exCfgName+" </a> " +
                    "<a onclick='exdelete(this);' data-id="+val.exCfgUuid+"><i class='fa fa-trash-o pull-right'></i></a></div>");
            });
        }else{$JuXue.n_success(r.msg);}
    });
}

function chooseTui(btn) {
    /**初始化推送部门配置**/
    $.getJSON(ctx+"dingConfig/initExName",{cExType:btn},function (r) {
        if (r.code == 0){
            var alldata = r.msg;$("#InterConfig").empty();
            $.each(alldata,function (index,val) {
                $("#InterConfig").append("<div class='fc-event'><a onclick='selectInternForDing(this);'>"+val.cExName+"</a> " +
                    "<a onclick='exdeleteInter(this);' data="+val.cExName+"><i class='fa fa-trash-o pull-right'></i></a></div>");
            });
        }else{$JuXue.n_success(r.msg);}
    });
}

/**选择部门**/
function selectDeptEx(btn) {
    var selectInter = sessionStorage.getItem("selectIntern");
    if (selectInter == ""){$JuXue.n_danger("请选择项目");return }
    $("#deptListExt").find("div").each(function () {$(this).removeClass("bg-blue-1");});
    $(btn).parent().addClass("bg-blue-1");
    var deptid = $(btn).attr("data");
    sessionStorage.setItem("selectDept",deptid);
    var type = sessionStorage.getItem("clickTabTui");
    /**加载部门的信息**/
    $.getJSON(ctx+"dingConfig/getUserInfoById",{"internet": selectInter,"deptid":deptid,"type":type},function (res) {
        if (res.code == 0){
            var data = res.data;
            $("#userDingjsTree_json").jstree("destroy");
            $("#userDingjsTree_json").jstree({
                "core": {"multiple": true,"data":data},
                "checkbox" : {"keep_selected_style" : false},
                "plugins" : [ "wholerow", "checkbox" ]
            });
            var msg  = res.msg;
            var deptMsg =  msg.dept;
            var interMsg = msg.intern;
            //TODO
            if (deptMsg == "0" || deptMsg == null){ $("#selectDiffBut").empty().append('<button type="button" onclick="changeTuiSongConfig(this)" data="1"  class="m-l-30 btn btn-xs btn-success">部门正常</button>');
            }else{$("#selectDiffBut").empty().append('<button type="button" onclick="changeTuiSongConfig(this)" data="0"  class="m-l-30 btn btn-xs btn-danger">部门暂停</button>');}

            if (interMsg == "0" || interMsg == null){ $("#selectDiffButInter").empty().append('<button type="button" onclick="changeInternConfig(this)" data="1"  class="m-l-30 btn btn-xs btn-success">项目正常</button>');
            }else{$("#selectDiffButInter").empty().append('<button type="button" onclick="changeInternConfig(this)" data="0"  class="m-l-30 btn btn-xs btn-danger">项目暂停</button>');}
        }else{$JuXue.n_danger("获取部门人员失败");}
    });
}
/**选择推送项目**/
function selectInternForDing(btn){
    $("#InterConfig").find("div").each(function () {
        $(this).removeClass("bg-blue-1");
    })
    $(btn).parent().addClass("bg-blue-1");
    var text = $(btn).text();
    sessionStorage.setItem("selectIntern",text);
}
/**新增**/
function selectInternAdd(btn) {
    var data_select = $(btn).attr("data-click");
    var type = sessionStorage.getItem("clickTabFen");
    if (data_select != "" && data_select != undefined){
        $.post(ctx+"exModal/exModalAdd",{exCfgName:data_select,exCfgType:type},function (r) {
            if (r.code == 0){
                $JuXue.n_success("新增成功");
                $("#exadd").append("<div class='fc-event'><a onclick='selectIntern(this);' type='"+type+"' data-id="+r.msg.exCfgUuid+" >"+data_select+" </a> " +
                    "<a onclick='exdelete(this);' data-id="+r.msg.exCfgUuid+"><i class='fa fa-trash-o pull-right'></i></a></div>");
            }else{$JuXue.n_success(r.msg);}
        });
    }
}
/**新增推送模板**/
function selectInternConfig(btn){
    var data_select = $(btn).attr("data-click");
    if (data_select != "" && data_select != undefined){
        var ble = false;
        $("#InterConfig").find("a").each(function () {var test = $(this).text();if (data_select == test ) {ble = true;return ;}});
        if (ble) {$JuXue.n_danger("已经存在此项目");return }
        var type = sessionStorage.getItem("clickTabTui");
        $.post(ctx+"dingConfig/dingConfigAdd",{cExName:data_select,cExType:type},function (r) {
            if (r.code == 0){
                $("#InterConfig").append("<div class='fc-event'><a onclick='selectInternForDing(this);' >"+data_select+"</a> " +
                    "<a onclick='exdeleteInter(this);' data="+data_select+"><i class='fa fa-trash-o pull-right'></i></a></div>");
            }else{$JuXue.n_success(r.msg);}
        });
    }
}
/**删除目录文件**/
function exdeleteInter(btn){
    var internet = $(btn).attr("data");
    var type = sessionStorage.getItem("clickTabTui");
    $.getJSON(ctx+"dingConfig/deleteExName",{"exName":internet,"type":type},function (res) {
        if (res.code == 0){
            $JuXue.n_success("删除成功");
            $(btn).parent().remove();}else {$JuXue.n_danger("删除失败");}
    });
}

/**删除**/
function exdelete(btn) {
    var data_select = $(btn).attr("data-id");
    if (data_select != "" && data_select != undefined){
        $JuXue.confirm({
            text: "确定删除选中模板？",
            confirmButtonText: "确定删除"
        }, function() {
            $.post(ctx + 'exModal/exModalDel', { "exCfgUuid": data_select }, function(r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                    $(btn).parent().remove();
                } else {$JuXue.n_danger(r.msg);}
            });
        });
    }
}
/**查询所有得信息**/
function selectIntern(btn) {
    var type = $(btn).attr("type");
    $("#exadd").find("div").each(function () {$(this).removeClass("bg-blue-1");});
    $(btn).parent().addClass("bg-blue-1");
    var data_select = $(btn).attr("data-id");
    $("#deptjsTree_json").jstree().deselect_all(true);
    $("#exConfig_form").empty();
    if (data_select != "" && data_select != undefined){
        $.post(ctx+"exModal/exModalRecordAll",{exCPuuid:data_select,exCType:type},function (r) {
            if (r.code == 0){
                $("#exConfig_form_id").val(data_select);
                var data = r.msg;
                var deptid = new Array();
                $.each(data,function (index,val) {
                    deptid.push(val.exCDept);
                    var  divapent = '<div class="input-group col-md-8 col-sm-8 m-3" id="dept'+val.exCDept+'" cuid ="'+val.exCUuid+'" data-id="'+val.exCDept+'">\n' +
                        '<span class="input-group-addon">'+val.exCDeptName+'</span>' +
                        '<input type="text" class="form-control" value="'+val.exCNum+'" placeholder="填写本月得分配数字"/>\n' +
                        '<span class="input-group-addon "><a href="#" onclick="downMove(this)"><i class="fa fa-arrow-circle-down"></i></a></span>' +
                        '<span class="input-group-addon"><a href="#" onclick="upMove(this)" ><i class="fa fa-arrow-circle-up"></i></a></span>' +
                        '<span class="input-group-addon"><a href="#" >已经分配:</a></span>' +
                        '<span class="input-group-addon"><a href="#" >'+val.exCCycles+' 个</a></span>' +
                        '<span class="input-group-addon">月度计划:</span>' +
                        '<input type="text" class="form-control" value="'+val.exCYtotle+'" placeholder="填写本月得分配总数字"/>\n' +
                        '</div>';
                    $("#exConfig_form").append(divapent);
                });
                $("#deptjsTree_json").jstree().select_node(deptid,true,true);
            }else{
                $JuXue.n_danger("加载数据失败");
            }
        });
    }
}

/**推广模板得下移**/
function downMove(btn) {
    var $div = $(btn).parent().parent();
    var lent = $div.next().length;
    if (lent === 0){$JuXue.n_danger("不能下移");return ;}
    //下移
    //1: 获取本节点所有得信息
    var divInfo = $div.html();
    //2：获取部门和部门id
    var nodeid = $div.attr("id");
    var deptid = $div.attr("data-id");
    var cuid = $div.attr("cuid");
    //3: 找到下一级 得下一级兄弟，并且删除 本节点
    /**获取inputnum得值**/
    var inputvaluenum = $(btn).parent().prev().val();
    /**获取数量总额**/
    var inputValueTotle = $(btn).parent().parent().children('input').eq(1).val();

    var sub$div = $div.next();
    //4: 新建一个节点  追加到下一级
    var  divapent = '<div class="input-group col-md-8 col-sm-8 m-3" id="'+nodeid+'" cuid="'+cuid+'" data-id="'+deptid+'"></div>';
    $div.remove();
    sub$div.after(divapent);
    $("#"+nodeid).append(divInfo);
    //追加节点
    $("#"+nodeid).children('input').eq(0).val(inputvaluenum);
    $("#"+nodeid).children('input').eq(1).val(inputValueTotle);
}
/**推广模板得上移动**/
function upMove(btn) {
    var $div = $(btn).parent().parent();
    var lent = $div.prev().length;
    if (lent === 0){$JuXue.n_danger("不能上移");return ;}
    //上移
    //1: 获取本节点所有得信息
    var divInfo = $div.html();
    //2：获取部门和部门id
    var nodeid = $div.attr("id");
    var deptid = $div.attr("data-id");
    var cuid = $div.attr("cuid");
    //3: 找到上一级 得上一级兄弟，并且删除 本节点
    /**获取input得值**/
    var inputvalue = $(btn).parent().prev().prev().val();
    /**获取数量总额**/
    var inputValueTotle = $(btn).parent().parent().children('input').eq(1).val();
    var sub$div = $div.prev();
    //4: 新建一个节点  追加到上一级
    var  divapent = '<div class="input-group col-md-8 col-sm-8 m-3" id="'+nodeid+'" cuid="'+cuid+'" data-id="'+deptid+'"></div>';
    $div.remove();
    sub$div.before(divapent);
    $("#"+nodeid).append(divInfo);
    //追加节点
    $("#"+nodeid).children('input').eq(0).val(inputvalue);
    $("#"+nodeid).children('input').eq(1).val(inputValueTotle);
}

/**保存服务器**/
function saveExConfig() {
    //1: 获取更新得项目
    var exConfigUuid = $("#exConfig_form_id").val();
    //if (exConfigUuid == "" || exConfigUuid == undefined){$JuXue.n_danger("请选择左侧得意向项目");return ;}
    //需要得数据{部门id, 配置数目，排序，新老数据得区别}
    var deptids = new Array();
    var cuids = new Array();
    var nums = new Array();
    var orders = new Array();
    var totle = new Array();
    $("#exConfig_form").find("div").each(function (index,val) {
        var deptid = $(this).attr("data-id");
        deptids.push(deptid);
        var cuid = $(this).attr("cuid");
        cuids.push(cuid);
        var num = $(this).children('input').eq(0).val();
        var totlell = $(this).children('input').eq(1).val();
        nums.push(num);
        orders.push(index+1);
        totle.push(totlell);
    });
    var deptidsstr = deptids.join(",");
    var cuidsstr = cuids.join(",");
    var numsstr = nums.join(",");
    var ordersstr = orders.join(",");
    var totlestr  = totle.join(",");
    var type = sessionStorage.getItem("clickTabFen");
    $.post(ctx+"exModal/exModalRecordedit",{exCUuid:cuidsstr,exCDept:deptidsstr,
        exCNum:numsstr,exCOrder:ordersstr,exCPuuid:exConfigUuid,exCYtotle:totlestr,exCType:type},function (r) {
        if (r.code == 0){$JuXue.n_success("保存成功");
        }else{$JuXue.n_danger(r.msg)}
    });
}

/**保存项目推送信息**/
function saveInternDing(){
    var deptid = sessionStorage.getItem("selectDept");
    if (deptid == ""){$JuXue.n_danger("请选择部门");return }
    var selectInter = sessionStorage.getItem("selectIntern");
    if (selectInter == ""){$JuXue.n_danger("请选择项目");return }
    var data = $("#userDingjsTree_json").jstree().get_selected(false);
    if (data.length <= 0){$JuXue.n_danger("请选择人员"); return;}
    var users = data.join(",");
    var type = sessionStorage.getItem("clickTabTui");
    $.post(ctx+"dingConfig/saveCdingConfig",{cExName:selectInter,cExDept:deptid,cExUser:users,cExType:type},function (res) {
       if (res.code === 0){$JuXue.n_success("设置成功");
       }else{$JuXue.n_danger("设置失败");
       }
    });
}
/**改变部门推送状态**/
function changeTuiSongConfig(btn) {
    var data = $(btn).attr("data");
    if (data == ""){$JuXue.n_danger("获取数据失败");return}
    var deptid = sessionStorage.getItem("selectDept");
    if (deptid == ""){$JuXue.n_danger("请选择项目");return }
    var selectInter = sessionStorage.getItem("selectIntern");
    if (selectInter == ""){$JuXue.n_danger("请选择项目");return }
    var type = sessionStorage.getItem("clickTabTui");
    $.post(ctx+"dingConfig/saveCdingConfig",{cExName:selectInter,cExDept:deptid,cExAithon:data,cExType:type},function (res) {
        if (res.code == 0){$JuXue.n_success("设置成功");
            if (data == "0"){ $("#selectDiffBut").empty().append('<button type="button" onclick="changeTuiSongConfig(this)" data="1"  class="m-l-30 btn btn-xs btn-success">部门正常</button>');
            }else{$("#selectDiffBut").empty().append('<button type="button" onclick="changeTuiSongConfig(this)" data="0"  class="m-l-30 btn btn-xs btn-danger">部门暂停</button>');}
        }else{$JuXue.n_danger("设置失败");}
    });
}
/**改变项目推送状态**/
function changeInternConfig(btn){
    var data = $(btn).attr("data");
    if (data == ""){$JuXue.n_danger("获取数据失败");return}
    var selectInter = sessionStorage.getItem("selectIntern");
    if (selectInter == ""){$JuXue.n_danger("请选择项目");return }
    var type = sessionStorage.getItem("clickTabTui");
    $.post(ctx+"dingConfig/saveCdingConfig",{cExName:selectInter,cExExname:data,cExType:type},function (res) {
        if (res.code == 0){$JuXue.n_success("设置成功");
            if (data == "0"){ $("#selectDiffButInter").empty().append('<button type="button" onclick="changeInternConfig(this)" data="1"  class="m-l-30 btn btn-xs btn-success">项目正常</button>');
            }else{$("#selectDiffButInter").empty().append('<button type="button" onclick="changeInternConfig(this)" data="0"  class="m-l-30 btn btn-xs btn-danger">项目暂停</button>');}
        }else{$JuXue.n_danger("设置失败");}
    });

}



